import time

from util.logger import get_logger
from curl_cffi import requests
from fake_useragent import UserAgent
import re,os,pathlib
url = "https://pic.netbian.com"
root = '../static/彼岸壁纸'
ua = UserAgent()
headers = {
    'Referer': url,
    'User-Agent': ua.random
}


session = requests.Session()

logger = get_logger(f'彼岸图库.log')

response = session.get(url,headers=headers,timeout=10)
response.encoding = 'gbk'

logger.info("爬取图片开始")

# print(response.status_code)
if response.status_code==200:
    time.sleep(1)
    suolue = re.findall(r'<a href="/tupian(.*?)" title="(.*?)" target="_blank">',response.text,re.S)
    for item in suolue:
        logger.info(f'正在爬取{item[1]}')
        # print(item)


        detail_url = url + '/tupian'+item[0]
        # print(detail_url)
        detail_pic = session.get(detail_url,headers=headers,)
        detail_pic = detail_pic.content.decode('gbk')

        print(detail_pic)

        detail = re.search('<img src="/uploads/allimg.*?" data-pic="(/uploads/allimg.*?)" alt=".*?" title=".*?">',detail_pic,re.S)

        pic_url = detail.group(1)
        pic_title = item[1]
        # print(f"地址:{pic_url} ；标题：{pic_title}")

        pic_real_url = url +  pic_url
        # print(f"下载地址：",pic_real_url)
        #
        # pic_resp = session.get(pic_real_url, headers={'Referer': pic_real_url, 'User-Agent': ua.random},timeout=15)
        #
        # with open(f"{root}/{pic_title}.jpg", "wb") as f:
        #     f.write(pic_resp.content)
        #     logger.info('爬取完毕，开始下一张')

















